<?php
/**
 * ooPHP - object oriented PHP framework
 * @author Kamil Mikolajczyk ( rego (at) gazeta (dot) pl )
 * @version 1.0
 *
 */
 
 include_once('printer.class.php');

/**
 * Represents hierarchical menu which can contain submenus or links
 * @author Kamil Mikolajczyk ( rego (at) gazeta (dot) pl )
 * @version 1.0
 */
 class Menu{
 
	/**
	 * Elements of menu
	 * @var array of Link and Menu
	 */
	var $elements = Array();
	
	/**
	 * Class attribute of menu
	 * @var string
	 */
	var $classa = null;
	
	/**
	 * Constructor
	 * Adds class if provided
	 */
	function Menu($class = null) 
	{
		$this -> classa = $class;
	}
	
	/**
	 * Adds new element to menu
	 * @param $e new element - Link or Menu
	 */
	function add($e)
	{
		$this -> elements[] = $e;
	}
	
	/**
	 * Renders menu with given indention
	 * @param $indents indention
	 */
	function render($indents = 0)
	{
		Printer::writeln('<ul'.($this -> classa ? ' class="'.$this -> classa.'"' : '').'>', $indents);
		foreach($this -> elements as $e)
		{
			Printer::writeln('<li>', $indents + 1);
			$e -> render($indents + 2);
			Printer::writeln('</li>', $indents + 1);
		}
		Printer::writeln('</ul>', $indents);
		//echo 'm'.count($this -> elements);
	}
	
	public static function renderJS($indents = 0)
	{
		Printer::writeln('<script type="text/javascript">', $indents);
		Printer::writeln("function expand(something){", $indents + 1);
		Printer::writeln("if(document.getElementById(something.id+'_sub').style.display != 'none')", $indents + 2);
		Printer::writeln("document.getElementById(something.id+'_sub').style.display = 'none';", $indents + 3);
		Printer::writeln("else", $indents + 2);
		Printer::writeln("document.getElementById(something.id+'_sub').style.display = 'list-item';", $indents + 3);
		Printer::writeln("}", $indents + 1);
		Printer::writeln("</script> ", $indents); 	
	}
 
 }
 
 /**
  * Represents expandable menu.
  * Requires js function which can be rendered using Menu::renderJS() method
  * @author Kamil Mikolajczyk ( rego (at) gazeta (dot) pl )
  * @version 1.0
  */
 class ExpandableMenu{
 
	/**
	 * Section's title
	 * @var string
	 */
	var $label = null;
	
	/**
	 * Section's id, must be unique
	 * @var string
	 */
	var $id = null;
	
	/**
	 * Class
	 * @var string
	 */
	var $classa = null;
	
	/**
	 * Child element
	 * @var Menu
	 */
	var $element = null;
	
	
	/**
	 *  TODO: icons
	 */
	
	/**
	 * Construtor
	 * Sets all fields
	 */
	function ExpandableMenu($label, $id, $element = null, $classa = null)
	{
		$this -> label = $label;
		$this -> classa = $classa;
		$this -> id = $id;
		$this -> element = $element;
	}
	
	/**
	 * Sets child element ($this -> element)
	 * @param $element element to be added
	 */
	function setContent($element)
	{
		$this -> element = $element;
	}
 
	/**
	 * Renders menu with given indention
	 * @param $indents indention
	 */
	function render($indents)
	{
		//echo 'e';
		Printer::writeln('<li><span onclick="expand(this);" id="'.$this -> id.'">'.$this -> label.'</span></li>', $indents);
		Printer::writeln('<li id="'.$this -> id.'_sub">', $indents);
		$this -> element -> render($indents + 1);
		Printer::writeln('</li>', $indents);
	}
	
 }
 
 /**
  * Represents <a> html element
  * @author Kamil Mikolajczyk ( rego (at) gazeta (dot) pl )
  * @version 1.0
  */
 class Link{
 
	var $url = "";
	var $label = "";
	var $alt = null;
	var $classa = null;
	
	/**
	 * Construtor
	 * Sets all fields
	 */
	function Link($url, $label, $class = null, $alt = null)
	{
		$this -> url = $url;
		$this -> label = $label;
		$this -> alt = $alt;
		$this -> classa = $class;
	}
	
	/**
	 * Gets Link in full html format
	 */
	function getLink()
	{
		$retVal = '<a href="'.$this -> url.'"'.($this -> alt ? ' alt="'.$this -> alt.'"' : "").($this -> classa ? ' class="'.$this -> classa.'"' : "").'>'.$this -> label.'</a>';
		return $retVal;
	}
 
	/**
	 * Renders menu with given indention
	 * @param $indents indention
	 */
	function render($indents = 0)
	{
		Printer::writeln($this -> getLink(), $indents);
	}
}
	?>
<html><style type="text/css">
<!--

-->
</style>


<script type="text/javascript">
    function expand(something)
        {
			if(document.getElementById(something.id+'_sub').style.display != 'none')
				document.getElementById(something.id+'_sub').style.display = 'none';
			else
				document.getElementById(something.id+'_sub').style.display = 'list-item';
        }
</script>

<body>
<? 
 $menuPortale = new Menu();
 $menuPortale -> add(new Link('http://www.onet.pl', 'onet'));
 $menuPortale -> add(new Link('http://www.wp.pl', 'wp'));
 $menuPortale -> add(new Link('http://www.gazeta.pl', 'gazeta'));
 
 $sectionPortale = new ExpandableMenu('PORTALE', 'portaleMenu', $menuPortale);
  
 $menuTorrenty = new Menu();
 $menuTorrenty -> add(new Link('http://torrentz.com', 'torrentz'));
 $menuTorrenty -> add(new Link('http://torrenty.org', 'torrenty'));
 $menuTorrenty -> add(new Link('http://www.revolution.tt', 'revtt'));
 
 $sectionTorrenty = new ExpandableMenu('TORRENTY', 'torrentyMenu', $menuTorrenty);
 
 $menuUtils = new Menu();
 $menuUtils -> add(new Link('http://www.gmail.com', 'gmail'));
 $menuUtils -> add(new Link('http://www.dobreprogramy.pl', 'dobreprogramy'));
 $menuUtils -> add($sectionTorrenty);
 
 $sectionUtils = new ExpandableMenu('PROGRAMY', 'utilsMenu', $menuUtils);
 
 $menuMain = new Menu();
 $menuMain -> add($sectionPortale);
 $menuMain -> add($sectionUtils);
 
 $sectionMenu = new ExpandableMenu('MENU', 'mainMenu', $menuMain);
 
 $menuRoot = new Menu();
 $menuRoot -> add($sectionMenu);
 
 $menuRoot -> render();
 

 ?>
 </body></html>